package org.yray.k8s;

import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.KubeConfig;
import io.kubernetes.client.util.credentials.AccessTokenAuthentication;

public class PodStatusExample {
    public static void main(String[] args) throws Exception {
        // 创建Kubernetes客户端
        ApiClient client = new ClientBuilder().setBasePath("http://192.168.127.129:8001").setVerifyingSsl(false)
                .setAuthentication(new AccessTokenAuthentication("")).build();
        Configuration.setDefaultApiClient(client);
        // 创建CoreV1Api对象
        CoreV1Api coreV1Api = new CoreV1Api();
        // 获取所有的pod
        V1PodList podList = coreV1Api.listNamespacedPod("default", null, null, null, null, null, null, null, null,null,null,null);
        for (V1Pod pod : podList.getItems()) {
            // 获取pod的状态
            String podName = pod.getMetadata().getName();
            String podStatus = pod.getStatus().getPhase();
            // 检查是否出现ImagePullBackOff状态
            if (podStatus.equals("ImagePullBackOff")) {
                System.out.println("Pod " + podName + " 状态为 ImagePullBackOff");
            } else {
                System.out.println("Pod " + podName + " 状态为 " + podStatus);
            }
        }
    }
}